package com.acgist.polaris.entity;

import java.io.IOException;
import java.io.InputStream;
import java.nio.ByteBuffer;

import com.acgist.polaris.Config;
import com.acgist.polaris.Tools;

/**
 * <p>文件实体</p>
 * 
 * @author acgist
 */
public class FileEntity extends StreamEntity {

	private static final long serialVersionUID = 1L;
	
	/**
	 * <p>修改时间戳</p>
	 */
	protected Long date;
	/**
	 * <p>文件路径</p>
	 */
	protected String path;
	
	/**
	 * @see #newFileEntity()
	 */
	private FileEntity() {
		super(Config.Type.FILE);
	}
	
	/**
	 * <p>获取文件实体</p>
	 * 
	 * @return 文件实体
	 */
	public static final FileEntity newFileEntity() {
		return new FileEntity();
	}

	/**
	 * <p>获取文件名称</p>
	 * 
	 * @return 文件名称
	 */
	public String fileName() {
		// TODO：实现
		return null;
	}
	
	/**
	 * {@inheritDoc}
	 * 
	 * <p>添加描述信息：修改时间戳、文件路径</p>
	 */
	@Override
	public byte[] buildDescription() {
		final byte[] superDescription = super.buildDescription();
		final byte[] dateBytes = Tools.longToBytes(this.date);
		final byte[] pathBytes = this.path.getBytes();
		final int length =
			superDescription.length +
			DESCRIPTION_LENGTH + dateBytes.length +
			DESCRIPTION_LENGTH + pathBytes.length;
		final ByteBuffer buffer = ByteBuffer.allocate(length);
		buffer
			.put(superDescription)
			.putInt(dateBytes.length).put(dateBytes)
			.putInt(pathBytes.length).put(pathBytes);
		return buffer.array();
	}

	@Override
	public StreamEntity buildDescription(InputStream input) throws IOException {
		super.buildDescription(input);
		this.date = Tools.bytesToLong(this.readDescriptionInfo(input));
		this.path = new String(this.readDescriptionInfo(input));
		return this;
	}
	
	public Long getDate() {
		return date;
	}

	public void setDate(Long date) {
		this.date = date;
	}

	public String getPath() {
		return path;
	}

	public void setPath(String path) {
		this.path = path;
	}

}
